<?php
/**
 * Created by PhpStorm.
 * User: 24339
 * Date: 2018/10/29
 * Time: 15:48
 */

namespace app\api\controller;

use think\Controller;
use think\Db;

class Api extends Controller
{
    public function getCompany($type)//首页面
    {
        if ($type == 'koubei') {
            $data = Db::table('company')->alias('c')->field('c.logo,c.name,c.desc,c.image')->join('comment m', "c.id = m.cmpid")->group('m.cmpid')->orderRaw('count(m.cmpid) desc')->limit(5)->select();
            foreach ($data as $k => $v) {
                $data[$k]['logo'] = 'http://172.16.101.42/renovation/public' . '/static/images/' . $data[$k]['logo'];
            }
            return json($data);
        } elseif ($type == 'case') {
            $data = Db::table('company')->alias('c')->field('c.logo,c.name,c.about')->join('case s', "c.id = s.cmpid")->group('s.cmpid')->orderRaw('count(s.cmpid) desc')->limit(5)->select();
            foreach ($data as $k => $v) {
                $data[$k]['logo'] = 'http://172.16.101.42/renovation/public' . '/static/images/' . $data[$k]['logo'];
            }
            return json($data);
        }
    }

    public function companyHead()//装修公司头部
    {
        $data = Db::table('company')->alias('c')->field('c.id,c.logo,c.name')->where('c.id', input('id'))->select();
        foreach ($data as $k => $v) {
            $data[$k]['logo'] = 'http://172.16.101.42/renovation/public' . '/static/images/' . $data[$k]['logo'];
        }
        $comment = Db::table('company')->alias('c')->join('comment m', "m.cmpid = c.id")->where('c.id', input('id'))->count('m.cmpid');
        $case = Db::table('company')->alias('c')->join('case a', "a.cmpid = c.id")->where('c.id', input('id'))->count('a.cmpid');
        return json(['data' => $data, 'case' => $case, 'comment' => $comment]);
    }

    public function companyInfo()
    {//获取装修公司信息
        $data = db('company')->where('id', input('id'))->select();
        foreach ($data as $k => $v) {
            $data[$k]['logo'] = 'http://172.16.101.42/renovation/public' . '/static/images/' . $data[$k]['logo'];
        }
        return json($data);
    }

    public function getCase()
    {//获取装修案例
        $data = db('case')->alias('a')->field('caseimg,caseinfo')->join('company c', "a.cmpid = c.id")->where('c.id', input('id'))->select();
        foreach ($data as $k => $v) {
            $data[$k]['caseimg'] = 'http://172.16.101.42/renovation/public' . '/static/img/' . $data[$k]['caseimg'];
        }
        $case = db('case')->alias('a')->join('company c', "a.cmpid = c.id")->where('c.id', input('id'))->count('a.cmpid');
        return json(['data' => $data, 'case' => $case]);
    }

    public function getComment()
    {//获取设计口碑
        $data = db('comment')->alias('m')->field('caseimg,caseinfo')->join('company c', "a.cmpid = c.id")->where('c.id', input('id'))->select();
        foreach ($data as $k => $v) {
            $data[$k]['caseimg'] = 'http://172.16.101.42/renovation/public' . '/static/img/' . $data[$k]['caseimg'];
        }
        $case = db('case')->alias('a')->join('company c', "a.cmpid = c.id")->where('c.id', input('id'))->count('a.cmpid');
        return json(['data' => $data, 'case' => $case]);
    }

    public function addUser()
    {//添加用户信息
        if (input('openid')) {//判断是否传openid过来
            // max=(a>b)?a:b;
            $data = [//定义需要插入的数据
                'openid' => input('openid'),//openid用户唯一识标
                'nickname' => input('nickname'),//用户昵称
                'headimg' => input('headimg'),//用户头像
                'createtime' => date("Y-m-d H:i:s"),//加入时间

            ];
            $d = Db('userinfo')->where('openid', $data['openid'])->column('openid');
            // var_dump($d);die;
            if ($d) {//用户已存在
                return json(['msg' => '老用户登录成功', 'openid' => $data['openid']]);//返回res:用户已存在
            } else {//用户不存在
                if (Db('userinfo')->insert($data)) {    //执行插入
                    return json(['msg' => '新用户加入成功', 'openid' => $data['openid']]);//发回res:join 结果 状态online 在线 和用户openid
                } else {
                    return json(array('res' => 'error'));
                }
            }
        }
    }

    public function reserve()
    {
        if ('cmpid' == input('cmpid')){
            $data = [
                'openid' => input('openid'),
                'nickname' => input('nickname'),
                'tel' => input('tel'),
                'cmpid' => input('cmpid'),
            ];
            $res = db('reserve')->insert($data);
            if ($res) {
                return json(['msg' => '预约公司成功', 'code' => 200]);
            } else {
                return json(['msg' => '预约公司失败', 'code' => 201]);
            }
        }
    }
}